'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 等級排行版
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var roleModel = require('../models/roleModel');
var unionGroupModel = require('../models/unionGroupModel');
var battleModel = require('../models/battleModel');
var soldierModel = require('../models/soldierModel.js');
var arenaModel = require('../models/arenaModel.js');
var playerManager = require('../libs/playerManager');
var dataApi = require('../libs/dataApi.js');
var roleService = require('./roleService');
var mailService = require('./mailService.js');
var soldierService = require('../services/soldierService.js');
var langUtil = require('../util/langUtil');
var moduleType = require('../consts/moduleType');
var operationType = require('../consts/operationType');

/**
 * 生成玩家排行版
 * @param {[type]} battle_list [description]
 */
exports.getHonorRank = function (cb) {
    _getHonorRankInfo(function (err, honor_list) {
        // 前50名玩家等級排行数据
        cb(null, honor_list);
    });
};

/**
 * 获取玩家排名
 * @param {[type]} current_role_rank [description]
 */
exports.getCurrentRoleRank = function (role_id, cb) {
    _getRoleRank(role_id, function (err, current_role_info) {
        // 当前玩家排名
        cb(null, current_role_info);
    });
};

/**
 * 发送军功排行奖励
 */
exports.sendHonorRankReward = function (cb) {
    var rank_length = dataApi.pvp_honor_rank.length();
    roleModel.getHonorRank(rank_length, function (err, top_rank_role) {
        var rank_num = 0;
        async.eachSeries(top_rank_role, function (role_item, callback) {
            rank_num++;
            var reward_info = dataApi.pvp_honor_rank.findById(rank_num);
            var player_manager = new playerManager(role_item.role_id);
            player_manager.getModel(['mail', 'accrued'], function (err) {
                var mail_model = player_manager.mail;
                var title = langUtil.getString('honor_rank_reward_mail_title');
                var content = langUtil.getString('honor_rank_reward_mail_content');
                var reward_list = [{
                    reward: reward_info.reward,
                    reward_type: reward_info.reward_type,
                    reward_num: reward_info.reward_num,
                    reward_value: reward_info.reward_value,
                    hole_num: reward_info.hole_num
                }];
                mailService.sendMail(mail_model, title, content, reward_list, moduleType.MDL_PVP, operationType.OPR_PVP_HONOR_RANK_REWARD);
                player_manager.save(callback);
            });
        }, function (err) {
            if (!!err) {
                console.error('sendHonorRankReward error:', err);
            }
            cb();
        });
    });
};

/**
 * 当前玩家排名
 * @param {[type]} reward_list [description]
 */
var _getRoleRank = function (role_id, cb) {
    var current_role_info = {};
    var role_model;
    var current_rank = 0;
    async.waterfall(
        [
            function (cb) {
                roleModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                role_model = model;
                roleModel.getCurrentRoleHonorRank(role_id, role_model.honor, cb);
            },
            function (rank, cb) {
                current_rank = rank;
                roleService.getRoleInfo(role_id, cb);
            },
            function (role_info, cb) {
                current_role_info = role_info;
                current_role_info.rank = current_rank;
                current_role_info.honor = role_model.honor;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            cb(null, current_role_info);
        }
    );
};

/**
 * 排行版相关信息
 * @param {[type]} reward_list [description]
 */
var _getHonorRankInfo = function (callback) {
    var rank_list = [];
    var top_fifty_role;
    var role_id_list = [];
    var soldier_map = {};
    var arena_map = {};
    var role_map = {};
    var where;
    async.waterfall(
        [
            function (cb) {
                roleModel.getHonorRank(50, cb);
            },
            function (list, cb) {
                top_fifty_role = list;
                _.forEach(top_fifty_role, function (item) {
                    role_id_list.push(item.role_id);
                });
                where = {
                    role_id: {
                        $in: role_id_list
                    }
                };
                soldierModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (soldier) {
                    soldier_map[soldier.role_id] = soldier;
                });

                arenaModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (arena) {
                    arena_map[arena.role_id] = arena;
                });

                roleService.getRoleList(role_id_list, cb);
            },
            function (list, cb) {
                _.forEach(list, function (role) {
                    role_map[role.role_id] = role;
                });

                var rank_num = 0;
                _.forEach(top_fifty_role, function (rank_info) {
                    rank_num++;
                    var role_id = rank_info.role_id;
                    var role_info = role_map[role_id];
                    var soldier_model = soldier_map[role_id];
                    var arena_model = arena_map[role_id];

                    role_info.honor = rank_info.honor;
                    role_info.rank = rank_num;
                    if (!!arena_model) {
                        role_info.arena_rank = arena_model.rank;
                    } else {
                        role_info.arena_rank = 0;
                    }
                    // role_info.soldier_list = soldierService.getSoldierInfoList(soldier_model.select_soldier_list, soldier_model);
                    role_info.fight_power = soldier_model.soldier_power;
                    rank_list.push(role_info);
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(null, []);
            }
            callback(null, rank_list);
        }
    );
};
